package net.mikoo.seals.boss.biz.services.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import net.mikoo.seals.boss.biz.manager.OssManager;
import net.mikoo.seals.boss.biz.mapper.boss.UpfileMapper;
import net.mikoo.seals.boss.biz.services.UpfileServices;
import net.mikoo.seals.boss.common.BaseServices;
import net.mikoo.seals.boss.common.exception.ParametersException;
import net.mikoo.seals.boss.common.exception.UploadException;
import net.mikoo.seals.boss.model.Upfile;

@Service
public class UpfileServicesImpl extends BaseServices implements UpfileServices {

	Logger logger = LoggerFactory.getLogger(UpfileServicesImpl.class);
	

	@Autowired
	private UpfileMapper upfileMapper;
	
	@Autowired
	private OssManager ossManager;

	@Override
	public Upfile upload(Upfile upfile) {
		
		Long fileSizeLimit =1024l;
		String imgLimitTypes= "*.jpg;*.jpeg;*.gif;*.bmp;*.png";
		

		if (upfile == null) {
			throw new ParametersException("upfile is null!");
		}

//		if (!imgLimitTypes.contains(upfile.getExtName())) {
//			throw new ParametersException(upfile.getExtName() + " Not allowed to upload");
//		}

		// 判断是否 超出大小
//		if (fileSizeLimit < upfile.getFileSize() / 1024) {
//			throw new ParametersException("文件" + upfile.getFileName() + "大小为" + upfile.getFileSize() + "超过设置的2M！");
//		}
		
		try {

			CommonsMultipartFile file = upfile.getFile();
			
			ossManager.upload(upfile.getFileUrl(), file.getInputStream());
			
			
			upfile.setFileUrl("http://gzws.oss-cn-hangzhou.aliyuncs.com/".concat(upfile.getFileUrl()));
			upfileMapper.insert(upfile);
			
			return upfile;
		} catch (Exception e) {
			logger.error("",e);
			throw new UploadException();
		}
		
		

	}

}
